package com.example.campusannouncement.controller;

import com.example.campusannouncement.feign.NewsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import feign.FeignException;

@Controller
@RequestMapping("/feign-test")
public class FeignTestController {
    private static final Logger logger = LoggerFactory.getLogger(FeignTestController.class);

    @Autowired
    private NewsClient newsClient;

    @GetMapping("")
    public String testPage() {
        return "feign-test";
    }

    @GetMapping("/search")
    @ResponseBody
    public String searchNews(@RequestParam String title) {
        try {
            logger.info("开始搜索新闻，标题: {}", title);
            String result = newsClient.searchNewsByTitle(title);
            logger.info("搜索成功，结果: {}", result);
            return result;
        } catch (FeignException.NotFound e) {
            logger.error("搜索失败：找不到服务或接口", e);
            return "错误：找不到新闻搜索服务或接口";
        } catch (FeignException e) {
            logger.error("搜索失败：" + e.getMessage(), e);
            return "错误：" + e.getMessage();
        } catch (Exception e) {
            logger.error("搜索过程中发生未知错误", e);
            return "错误：搜索过程中发生未知错误";
        }
    }
} 